package com.cleaningcloud.erp.controller;

import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import com.cleaningcloud.erp.domain.ErpOutbound;
import com.cleaningcloud.erp.domain.dto.ErpOutboundDto;
import com.cleaningcloud.erp.domain.excelExport.OutboundExcelExport;
import com.cleaningcloud.erp.domain.vo.ErpOutboundDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpOutboundVo;
import com.cleaningcloud.erp.service.IErpOutboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 销售出库Controller
 *
 * @author cleaningcloud
 * @date 2024-10-02
 */
@RestController
@RequestMapping("/ErpOutbound")
public class ErpOutboundController extends BaseController {
    @Autowired
    private IErpOutboundService erpOutboundService;

    /**
     * 查询销售出库列表
     */
    @RequiresPermissions("erp:ErpOutbound:list")
    @GetMapping("/list")
    public TableDataInfo list(ErpOutbound erpOutbound) {
        startPage();
        List<ErpOutboundVo> list = erpOutboundService.selectErpOutboundList(erpOutbound);
        return getDataTable(list);
    }

    /**
     * 导出销售出库列表
     */
    @RequiresPermissions("erp:ErpOutbound:export")
    @Log(title = "销售出库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpOutbound erpOutbound) {
        List<ErpOutboundVo> list = erpOutboundService.selectErpOutboundList(erpOutbound);
        ExcelUtil<ErpOutboundVo> util = new ExcelUtil<ErpOutboundVo>(ErpOutboundVo.class);
        util.exportExcel(response, list, "销售出库数据");
    }

    /**
     * 导出销售出库明细列表
     */
    @RequiresPermissions("erp:ErpOutbound:export")
    @Log(title = "销售出库", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDetails/{outboundId}")
    public void exportDetails(HttpServletResponse response, @PathVariable Long outboundId) {
        ErpOutboundVo outboundVo = erpOutboundService.selectErpOutboundByOutboundId(outboundId);
        List<OutboundExcelExport> list = new ArrayList<>();
        for (ErpOutboundDetailsVo erpOutboundDetailsVo : outboundVo.getDetailsList()) {
            OutboundExcelExport outboundExcelExport = new OutboundExcelExport();
            outboundExcelExport.setOutboundNo(outboundVo.getOutboundNo());
            outboundExcelExport.setOddNo(outboundVo.getOddNo());
            outboundExcelExport.setCustomerName(outboundVo.getCustomerName());
            outboundExcelExport.setWareName(outboundVo.getWareName());
            outboundExcelExport.setOutboundType(outboundVo.getOutboundType());
            outboundExcelExport.setAnswerScheduledReceipt(outboundVo.getAnswerScheduledReceipt());
            outboundExcelExport.setOutboundNumber(outboundVo.getOutboundNumber());
            outboundExcelExport.setOutboundStatus(outboundVo.getOutboundStatus());
            outboundExcelExport.setOutboundPersonName(outboundVo.getOutboundPersonName());
            outboundExcelExport.setNickName(outboundVo.getNickName());
            outboundExcelExport.setPhonenumber(outboundVo.getPhonenumber());
            outboundExcelExport.setOutboundTime(outboundVo.getOutboundTime());
            outboundExcelExport.setCreateName(outboundVo.getCreateName());
            outboundExcelExport.setCommodityNo(erpOutboundDetailsVo.getCommodityNo());
            outboundExcelExport.setCommodityName1(erpOutboundDetailsVo.getCommodityName1());
            outboundExcelExport.setCommodityType(erpOutboundDetailsVo.getCommodityType());
            outboundExcelExport.setUnitName1(erpOutboundDetailsVo.getUnitName1());
            outboundExcelExport.setSymbolPrice(erpOutboundDetailsVo.getSymbolPrice());
            outboundExcelExport.setSaleNumber(erpOutboundDetailsVo.getSaleNumber());
            outboundExcelExport.setRealityNumber(erpOutboundDetailsVo.getRealityNumber());
            list.add(outboundExcelExport);
        }
        ExcelUtil<OutboundExcelExport> util = new ExcelUtil<OutboundExcelExport>(OutboundExcelExport.class);
        util.exportExcel(response, list, "销售出库数据");
    }

    /**
     * 获取销售出库详细信息
     */
    @RequiresPermissions("erp:ErpOutbound:query")
    @GetMapping(value = "/{outboundId}")
    public AjaxResult getInfo(@PathVariable("outboundId") Long outboundId) {
        return success(erpOutboundService.selectErpOutboundByOutboundId(outboundId));
    }

    /**
     * 新增销售出库
     */
    @RequiresPermissions("erp:ErpOutbound:add")
    @Log(title = "销售出库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpOutbound erpOutbound) {
        return toAjax(erpOutboundService.insertErpOutbound(erpOutbound));
    }

    /**
     * 修改销售出库
     */
    @RequiresPermissions("erp:ErpOutbound:edit")
    @Log(title = "销售出库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpOutboundDto erpOutbound) {
        return toAjax(erpOutboundService.updateErpOutbound(erpOutbound));
    }

    /**
     * 删除销售出库
     */
    @RequiresPermissions("erp:ErpOutbound:remove")
    @Log(title = "销售出库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{outboundIds}")
    public AjaxResult remove(@PathVariable Long[] outboundIds) {
        return toAjax(erpOutboundService.deleteErpOutboundByOutboundIds(outboundIds));
    }

    /**
     * @Author Shaw
     * @Description 收货操作
     * @date: 2025-04-06 15:12
     * @param: //TODO
     */
    @RequiresPermissions("erp:ErpOutbound:edit")
    @Log(title = "销售出库收货", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/harvest")
    public AjaxResult harvest(@RequestBody ErpOutboundDto erpOutbound) {
        erpOutbound.setIsHarvest(1);//收货
        erpOutbound.setHarvestTime(DateUtils.getNowDate());//收货时间
        erpOutbound.setDeliverTime(DateUtils.getNowDate());//送货时间
        return toAjax(erpOutboundService.updateErpOutbound(erpOutbound));
    }
}
